﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using System.Reflection;
using System.Collections.ObjectModel;

namespace WPFInterview.Generic
{
    // public class NameList : ObservableCollection<PersonName>
    public class NameList: Collection<PersonName>
    {
        public NameList()
            : base()
        {
            Add(new PersonName("Willa", "Cather"));
            Add(new PersonName("Isak", "Dinesen"));
            Add(new PersonName("Victor", "Hugo"));
            Add(new PersonName("Jules", "Verne"));
        }
    }

    public class PersonName
    {
        private string firstName;
        private string lastName;

        public PersonName(string first, string last)
        {
            this.firstName = first;
            this.lastName = last;
        }

        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }
    }

    /// <summary>
    /// Interaction logic for GenericQuestion.xaml
    /// </summary>
    public partial class GenericQuestion : UserControl
    {
        Collection<PersonName> collection = new Collection<PersonName>();
        ArrayList arraylist = new ArrayList();
        int numer = 10;
        bool test = false;

        public GenericQuestion()
        {
            InitializeComponent();
            NameList nameList = new NameList();

            collection.Add(new PersonName("a", "b"));
            collection.Add(new PersonName("c", "d"));

            arraylist.Add(new PersonName("a", "b"));
            arraylist.Add(new PersonName("c", "d"));

            // _genericList.ItemsSource = collection;
            _genericList.ItemsSource = arraylist;
            _genericList.DisplayMemberPath = "FirstName";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            numer++;
            collection.Add(new PersonName(String.Format("{0}", numer), String.Format("{0}", numer)));            
            _genericList.ItemsSource = collection;
            

            test = !test;
            if (test)
                _genericList.DisplayMemberPath = "LastName";
            else
                _genericList.DisplayMemberPath = "FirstName";

            IEnumerator enumerator = collection.GetEnumerator();
            while (enumerator.MoveNext())
            {
                PersonName name = (PersonName)enumerator.Current;
                MessageBox.Show(name.FirstName);
            }
        }
    }

    public class GenericTest : IQuestion
    {
        public string TestName
        {
            get { return ".NET - Generics"; }
        }

        public UserControl getUserControl()
        {
            return new GenericQuestion();
        }
    }
}
